package com.rm.zeyou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.Message;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.mapper.MessageMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.vo.request.SelectMessageListVo;
import com.rm.zeyou.vo.response.SelectMessageListResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-13 09:51:59
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Resource
    private MessageMapper messageMapper;

    @Override
    public ApiRestResponse<PageInfo<SelectMessageListResponseVo>> selectMessageList(User user, SelectMessageListVo requestVo) {
        return ApiRestResponse.success(PageHelper.startPage(requestVo.getPageNum(),requestVo.getPageSize()).doSelectPageInfo(()->{
            messageMapper.selectMessageList(user.getId());
        }));
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> readMessage(User user, Long messageId) {
        return ApiRestResponse.success(messageMapper.readMessage(messageId));
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> deleteMessage(User user) {
        return ApiRestResponse.success(messageMapper.deleteMessageAll(user.getId()));
    }
}
